<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
    <title>优惠券 - 肯德基风格</title>
    <style>
        /* 肯德基经典配色：红色#E31837，黄色#FFC400，黑色#000 */
        body {
            margin: 0;
            font-family: 'Arial', sans-serif;
            background-color: #f8f8f8;
            color: #333;
        }

        .header {
            background-color: #E31837;
            color: white;
            text-align: center;
            padding: 1.5rem;
            font-size: 2rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .category-bar {
            justify-content: flex-start;
            padding: 1rem 3rem;
            background-color: white;
            border-bottom: 2px solid #FFC400;
        }

        .category-button {
            padding: 0.6rem 1.2rem;
            border: 2px solid #E31837;
            border-radius: 25px;
            font-size: 1rem;
            font-weight: bold;
        }

        .category-button:hover {
            background-color: #E31837;
            color: white;
            transform: scale(1.03);
        }

        .coupon-container {
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            padding: 2rem 3rem;
        }

        .coupon-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            background-color: white;
            transition: all 0.3s ease;
            display: flex;
            overflow: hidden;
            position: relative;
        }

        .coupon-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 6px 18px rgba(0,0,0,0.12);
        }

        .coupon-info {
            padding: 1.5rem;
            flex: 1;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .coupon-name {
            font-size: 1.2rem;
            color: #000;
            margin-bottom: 0.5rem;
        }

        .coupon-price {
            color: #E31837;
            font-weight: bold;
            font-size: 1.1rem;
            margin-bottom: 0.3rem;
        }

        .coupon-type,
        .coupon-start-time,
        .coupon-end-time,
        .coupon-valid-day {
            color: #666;
            font-size: 0.85rem;
            line-height: 1.3;
        }

        .coupon-action {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 90px;
            padding: 0 1rem;
        }

        .coupon-button {
            background-color: #E31837;
            color: white;
            border: none;
            border-radius: 25px;
            padding: 0.5rem 0.8rem;
            font-size: 0.9rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.2s ease;
            white-space: nowrap;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            transform: translateX(-5px);
        }

        .coupon-button:hover {
            background-color: #C70000;
            transform: translateX(-5px) translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        }

        .coupon-button:active {
            transform: translateX(-5px) translateY(0);
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        /* 新增领取成功状态样式 */
        .coupon-button.received {
            background-color: #999;
            cursor: not-allowed;
            transform: translateX(-5px);
        }

        /* 加载中动画 */
        .loading-spinner {
            display: inline-block;
            width: 12px;
            height: 12px;
            border: 2px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: white;
            animation: spin 1s linear infinite;
            margin-right: 5px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
<header class="header">
    <h1>会员专享优惠券</h1>
</header>
<div id="app">
    <ul class="coupon-container">
        <li v-for="(coupon, index) in couponList" :key="index">
            <div class="coupon-card">
                <div class="coupon-info">
                    <div class="coupon-name">{{ coupon.couponName }}</div>
                    <div class="coupon-price">¥{{ coupon.couponPrice }}</div>
                    <div class="coupon-type">类型：{{ coupon.couponType }}</div>
                    <div class="coupon-start-time">有效期：{{ coupon.couponStartTime }} - {{ coupon.couponEndTime }}</div>
                    <div class="coupon-valid-day">剩余{{ coupon.couponValidDays }}天</div>
                </div>
                <div class="coupon-action">
                    <button
                            class="coupon-button"
                            :class="{ 'received': coupon.received }"
                            :disabled="coupon.received || coupon.loading"
                            @click="receiveCoupon(coupon)">
                        <span v-if="coupon.loading"><span class="loading-spinner"></span> 领取中</span>
                        <span v-else-if="coupon.received">已领取</span>
                        <span v-else>立即领取</span>
                    </button>
                </div>
            </div>
        </li>
    </ul>
</div>
<script>
    const vm = new Vue({
        el: "#app",
        data: {
            couponList: []
        },
        mounted() {
            this.getAllCoupon()
        },
        methods: {
            // 获取所有优惠券
            getAllCoupon() {
                axios.get("coupon")
                    .then(response => {
                        // 确认返回的数据结构正确
                        console.log("获取优惠券列表:", response.data.data);

                        // 初始化优惠券状态
                        this.couponList = response.data.data.map(coupon => ({
                            ...coupon,
                            received: false, // 是否已领取
                            loading: false   // 是否正在加载
                        }));
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },

            // 领取优惠券
            receiveCoupon(coupon) {
                // 如果已经领取或正在加载，直接返回
                if (coupon.received || coupon.loading) return;

                // 设置加载状态
                coupon.loading = true;

                console.log("优惠券的ID",coupon.couponId)

                // 发送请求到后端API
                axios.post("mycoupon", {
                    custId: 1,  // 实际项目中应从会话或令牌中获取用户ID
                    couponId: coupon.couponId, // 假设优惠券有id字段
                    couponNumber: 123456,
                    couponStartTime: coupon.couponStartTime,
                    couponEndTime: coupon.couponEndTime
                })
                    .then(response => {
                        // 更新优惠券状态
                        coupon.received = true;

                        // 显示成功消息（实际项目中可使用更友好的提示）
                        console.log("优惠券领取成功！");

                        // 如果需要更新用户优惠券数量，可以在这里触发相关操作
                        // this.updateUserCouponCount();
                    })
                    .catch(error => {
                        console.log("优惠券领取失败:", error);
                        // 显示错误消息（实际项目中可使用更友好的提示）
                    })
                    .finally(() => {
                        // 无论成功或失败，都关闭加载状态
                        coupon.loading = false;
                    });
            }
        }
    })
</script>
</body>
</html>